<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/9/6
 * Time: 13:59
 */
namespace app\admin\controller;

use app\middleware\AdminCheck;
use think\facade\Db;

class SkillChapter {

    protected $middleware = [
        AdminCheck::class => ['except' 	=> [''] ]
    ];



    /**
     *   @SWG\Post(
     *     path="/admin/skillChapter/add",
     *     tags={"后台考试技巧相关"},
     *     summary="添加技巧章节",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="skill_paper_id,title_，content_，type_",
     *      type="string",
     *      description="skill_paper_id（技巧等级的id）,title_（技巧章节标题），content_（章节内容描述），type_（类型：1为判断题，2为选择题）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 添加技巧章节
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        validateParam('skill_paper_id,title_,content_,type_','skill_paper_id,title_,content_,type_');
        //通过技巧等级获取城市
        $skill_paper = Db::name('skill_paper')
            ->where('id_',input('skill_paper_id'))
            ->find();
        $data['city_']=$skill_paper['city_'];
        $data['skill_paper_id'] = input('skill_paper_id');
        $data['title_']=input('title_');
        $data['content_']=input('content_');
        $data['type_'] = input('type_');
        Db::name('skill_chapter')->insert($data);
        return success('添加成功');
    }




    /**
     *   @SWG\Post(
     *     path="/admin/skillChapter/getList",
     *     tags={"后台考试技巧相关"},
     *     summary="技巧章节列表",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="pageNo,pageSize",
     *      type="string",
     *      description="pageNo(第几页),pageSize（一页多少条数据）,city_（城市）,skill_paper_id(技巧等级id)"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 章节列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        validateParam('city_,pageNo,pageSize','city_,pageNo,pageSize');
        $param=input();
        $where[]=['city_','=',$param['city_']];
        if(!empty($param['skill_paper_id'])){
            $where[]=['skill_paper_id','=',$param['skill_paper_id']];
        }
        //获取总数
        $total=Db::name('skill_chapter')
            ->where($where)
            ->count();
        $offset=makeOffset($param['pageNo'],$param['pageSize']);

        $list=Db::name('skill_chapter')
            ->where($where)
            ->limit($offset,$param['pageSize'])
            ->select();
        return success('技巧章节列表',['total'=>$total,'list'=>$list]);
    }



    /**
     *   @SWG\Post(
     *     path="/admin/skillChapter/edit",
     *     tags={"后台考试技巧相关"},
     *     summary="编辑技巧章节",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=true,
     *      name="id_,skill_paper_id,title_，content_，type_",
     *      type="string",
     *      description="id_,skill_paper_id（技巧等级的id）,title_（技巧章节标题），content_（章节内容描述），type_（类型：1为判断题，2为选择题）"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 编辑技巧章节
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(){
        validateParam('id_,skill_paper_id,title_,content_,type_','id_,skill_paper_id,title_,content_,type_');
        //通过技巧等级获取城市
        $skill_paper = Db::name('skill_paper')
            ->where('id_',input('skill_paper_id'))
            ->find();
        $data['city_']=$skill_paper['city_'];
        $data['skill_paper_id'] = input('skill_paper_id');
        $data['title_']=input('title_');
        $data['content_']=input('content_');
        $data['type_'] = input('type_');
        Db::name('skill_chapter')
            ->where('id_',input('id_'))
            ->update($data);
        return success('编辑成功');
    }




    /**
     *   @SWG\Post(
     *     path="/admin/skillChapter/delete",
     *     tags={"后台考试技巧相关"},
     *     summary="删除技巧章节（单个删除）",
     *     consumes={"application/json"},
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *      in="query",
     *      required=false,
     *      name="id_",
     *      type="string",
     *      description="id_"),
     *     @SWG\Response(response="200",description="ok")
     *  )
     */
    /**
     * 删除技巧章节
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete(){
        validateParam('id_','id_');
        //首先判断技巧等级下面是否存在技巧章节
        $question = Db::name('skill_chapter_question')
            ->where('skill_chapter_id',input('id_'))
            ->find();
        if(!empty($question)){
            return error('技巧章节下存在关联的题目，不可删除');
        }
        Db::name('skill_chapter')
            ->where('id_','=',input('id_'))
            ->delete();
        return success('删除成功');
    }



}